package cn.cvs.service;

import cn.cvs.mapper.SysUserPlusMapper;
import cn.cvs.pojo.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserPlusServiceImpl extends ServiceImpl<SysUserPlusMapper, SysUser> implements SysUserPlusService {
    @Resource
    SysUserPlusMapper userPlusMapper;

    public List<SysUser> getUserByUsrName(String usrName) {
   /*     LambdaQueryWrapper<SysUser> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsrName,usrName);
*/
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usr_name", usrName);
        return super.list(queryWrapper);
    }

    public SysUser login(String usrName, String password) {

        List<SysUser> userList = this.getUserByUsrName(usrName);
        SysUser loginUser = null;
        if (null != userList && userList.size() > 0) {
            loginUser = userList.stream().filter(sysUser -> sysUser.getUsrPassword().equals(password)).findAny().orElse(null);
        }
        return loginUser;
    }

    @Override
    public SysUser login(String usrName) {
        // SysUser sysUser=this.getUserByUsrName(usrName);//查单表
        // SysUser sysUser=userMapper.getByUsrNameSysUser(usrName); //自定义SQL,查role_name
        SysUser sysUser = userPlusMapper.getByUsrNameSysUserAndRole(usrName); //自定义Results 查role对象
        return sysUser;
    }

    @Override
    public String encryptPassword(Object plaintextPassword) throws IllegalArgumentException {
        String salt = "czkt";
        Md5Hash md5Hash = new Md5Hash(plaintextPassword, salt, 2);
        return md5Hash.toString();
    }

    @Override
    public IPage<SysUser> selectPage(Page<SysUser> page, QueryWrapper<SysUser> wrapper) {
        return userPlusMapper.selectPage(page, wrapper);
    }
}
